package com.recycle.recycledemo.recycle;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by lwg on 2017/12/22.
 */

public abstract class CommonAdapter<T> extends RecyclerView.Adapter<CommonViewHolder> {

    private OnItemClickListener listener;
    private Context context;
    private int layoutId;
    private List<T> data;

    public CommonAdapter(Context context, int layoutId, List<T> data) {
        this.context = context;
        this.layoutId = layoutId;
        this.data = data;
    }

    @Override
    public CommonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return CommonViewHolder.getInstance(context,layoutId,null);
    }

    @Override
    public void onBindViewHolder(CommonViewHolder holder, final int position) {
        if(listener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.setOnItemClickListener(position,data.get(position));
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    listener.setOnLongItemClickListener(position,data.get(position));
                    return true;
                }
            });
        }
        convert(holder,data.get(position),position);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public abstract void convert(CommonViewHolder holder,T t,int position);

    interface OnItemClickListener<L>{
        abstract void setOnItemClickListener(int Position,L l);
        abstract void setOnLongItemClickListener(int Position,L l);
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener = listener;
    }
}
